package com.easy.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.exception.CustomException;
import com.easy.user.mapper.FeedbackMapper;
import com.easy.user.pojo.Feedback;
import com.easy.user.service.IFeedbackService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    public boolean addFeedback(Feedback feedback) {
        int sum = 0;
        try {
            sum = feedbackMapper.insert(feedback);
        } catch (Exception e) {
            throw new CustomException("添加反馈信息失败", "BC020");
        }
        return sum > 0;
    }

    @Override
    public Feedback getFeedback(Long id) {
        Feedback feedback = null;
        try {
            LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Feedback::getId, id);
            feedback = feedbackMapper.selectOne(wrapper);
        } catch (Exception e) {
            throw new CustomException("获取反馈信息失败", "BC021");
        }
        return feedback;
    }

    @Override
    public Page<Feedback> getFeedbackList(int page, int pageSize, Long uid) {
        Page<Feedback> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Feedback::getUserId, uid);
        try {
            this.page(pageInfo, wrapper);
        } catch (Exception e) {
            throw new CustomException("获取反馈信息列表失败", "BC022");
        }
        return pageInfo;
    }
}
